package com.cssnj.portal.web.home;

import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.common.TreeService;
import com.cssnj.portal.service.home.HomeService;
import com.cssnj.portal.service.user.UserService;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@Controller
public class SearchController {

    @Autowired
    private Environment environment;

    @Autowired
    TreeService treeService;

    @RequestMapping({"/search.do"})
    public ModelAndView search(String searchText, HttpServletRequest httpServletRequest) throws SQLException, BaseException {

        ModelAndView modelAndView = new ModelAndView("home/search");
        JsonRequest jsonRequest = new JsonRequest();
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        jsonRequest.getReqData().put("userInfo", userInfo);
        jsonRequest.getReqData().put("searchText", searchText);
        jsonRequest.setHandleCode("searchGnByMc");
        JsonResponse jsonResponse = treeService.execute(jsonRequest);
        modelAndView.addObject("list", jsonResponse.getRepData().get("list"));
        modelAndView.addObject("searchText", searchText);
        return modelAndView;

    }

}
